﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpSphinx.Util.Configuration;

namespace SharpSphinx.Linguist.Acoustic
{
   
    public class UnitManager : IConfigurable 
    {
        public const String SILENCE_NAME = "SIL";
        private static int SILENCE_ID = 1;


        public static Unit SILENCE = new Unit(SILENCE_NAME, true, SILENCE_ID);


        private  Dictionary<string, Unit> Map = new Dictionary<string, Unit>();

        private int NextID = SILENCE_ID + 1;
       // private Logger logger;

        public override void newProperties(PropertySheet ps)
        {
           
        }

        public UnitManager()
        { Map.Add(SILENCE_NAME, SILENCE); }

       /// <summary>
        /// Gets or creates a unit from the unit pool
       /// </summary>
       /// <param name="name"></param>
       /// <param name="Filler"></param>
       /// <param name="context"></param>
       /// <returns></returns>
        public Unit getUnit(String name, bool Filler, Context context)
        {
            Unit unit; 
            Map.TryGetValue(name,out unit);

            if (context == Context.Empty_Context)
            {
                if (unit == null)
                {
                    unit = new Unit(name, Filler, NextID++);
                    Map.Add(name, unit);
                }
            }
            else
            {
                unit = new Unit(unit, Filler, context);
            }
            return unit;
        }


        public Unit getUnit(String name, bool Filler)
        {
            return getUnit(name, Filler, Context.Empty_Context);
        }

        public Unit getUnit(String name)
        {
            return getUnit(name, false, Context.Empty_Context);
        }

    }
}
